#ifndef mbObj

#include "mbapp.h"
#include "com_def.h"
#include <rtthread.h>
#include "hal_data.h"
#include <rtdevice.h>

#define mbRxEnable()
#define mbTxEnable()
mb_obj_type mbObj;
#define mbPORT UART3
#define mbURT_IRQn UART3_IRQn
static void mb_cb_set(void)
{
}
static void mb_port_hal_cfg(void) {}
#endif

static void mb_cb_set(void);
static void mbObjInit(mb_mode_type mbMode);
static void mb_port_Init(mb_mode_type mbMode, uint32_t ulBaudRate, mb_parity_type eParity);
static void mb_port_send(void);
static void mb_port_hal_cfg(void);

/*****************************************************************************
 * @brief UART OPT
 *****************************************************************************/

/*****************************************************************************
 * @brief mb_byte_send
 * @param   dat
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/


/*****************************************************************************
 * @brief   	com modle init .
 * @param   none
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
static void mb_port_Init(mb_mode_type mbMode, uint32_t ulBaudRate, mb_parity_type eParity)
{
	mb_port_hal_cfg();

	mbObjInit(mbMode);


	// rtu timer
	if (mbObj.run_st.bits.rtu_mode == 0)
	{
		mbObj.rtu_timer_sv = 240;
		mbObj.timeout_set = 500;
	}
	else
	{
		if (ulBaudRate > 19200)
		{
			mbObj.rtu_timer_sv = 3;
		}
		else
		{
			mbObj.rtu_timer_sv = (com_timer_cal(ulBaudRate, (35 * 11 + 9) / 10, 300, 12000) + 99) / 100;
		}
	}
}


/*****************************************************************************
 *
 * @brief   Send data.
 *
 * @param   none
 *
 * @return  none
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
static void mb_port_send(void)
{
	rt_device_write(rt_device_find("uart7"), -1, mbObj.adu_buff, mbObj.snd_size);
	
	mb_send_end(&mbObj);
}

/*****************************************************************************
 *
 * @brief   mb object init.
 *
 * @param   none
 *
 * @return  TURE
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
static void mbObjInit(mb_mode_type mbMode)
{
	mb_obj_init(&mbObj);
	mbObj.adu_send = &mb_port_send;

	mb_cb_set();

	mbObj.master.err_cnt = 0;

	// SLAVE FUNCTION INIT
	if (MB_RTU_MASTER == mbMode || mbMode == MB_ASCII_MASTER)
	{
		mbObj.run_st.bits.master_mode = 1;
	}
	else
	{
		mbObj.run_st.bits.master_mode = 0;
	}
	
	if (MB_RTU_MASTER == mbMode || mbMode == MB_RTU_SLAVE)
	{
		mbObj.run_st.bits.rtu_mode = 1;
	}
	else
	{
		mbObj.run_st.bits.rtu_mode = 0;
	}
}

/*****************************************************************************
 * @brief   sim_mbport_keep
 * @param   none
 * @return  none
 *****************************************************************************/
#ifndef mbObj
void sim_mbport_keep(void)
{
	mb_port_Init(MB_RTU_SLAVE, 9600, MB_PAR_NONE);
}
#endif